<?php
header('Access-Control-Allow-Origin:*');
$data_arr = array();
if($_FILES){
    $filename = './import_controller/'.time().rand().'.txt';
    move_uploaded_file($_FILES['controller_file']['tmp_name'],$filename);
    sleep(0.5);
    $file = fopen($filename,'r');
    while($row = fgets($file)){   // 这个地方注意，被读取文件的一行不能太长
        array_push($data_arr,$row);
    }
    $db_con = mysqli_connect('localhost','root','');
    $result = mysqli_query($db_con,'set names utf8');
    $result = mysqli_query($db_con,'use sx_control');
    // $pro_id = $_COOKIE['edit_project_id'];
    $table_name = 'controller'.$_POST['edit_project_id'];
   
    $data = json_decode($data_arr[0],JSON_UNESCAPED_UNICODE);

    for($i=1;$i<=count($data_arr);$i++){
        $data = json_decode($data_arr[$i],JSON_UNESCAPED_UNICODE);
        $row_type = $data['type'];
        $row_name = $data['name'];
        $row_address = $data['address'];
        $row_data_type = $data['data_type'];
        $row_value = $data['value'];
        $row_priority = $data['priority'];
        $row_room = $data['room'];
        $row_room_desc = $data['room_desc'];
        $sql = "insert into $table_name values(default , '$row_type', '$row_name','$row_address','$row_data_type','$row_value','$row_priority','$row_room','$row_room_desc')";
        $result = mysqli_query($db_con,$sql);
    }
    mysqli_close($db_con);
    fclose($file);
}
?>